﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using IHISCETeim.BLL;

using NPOI.HSSF.UserModel;
using NPOI.HPSF;
using NPOI.POIFS.FileSystem;
using NPOI.SS.UserModel;
using NPOI.SS.Util;
using IHISCETeim.Library;
using IHISCETeim.Model;

namespace IHISCETeim.Web.Export.MacModel
{
    public partial class ExportOJobList : ExportPageBase
    {
        protected override string xlsTemplateFileName
        {
            get
            {
                return "生产_机器库存一览表.xls";
            }
        }
        protected override string SheetName
        {
            get
            {
                return "Sheet1";
            }
        }

        #region 属性 (查询条件)


        protected string CommonFiled
        {
            get
            {
                return GetRequestValue<string>("CommonFiled", "");

            }
        }
        protected string CommonValue
        {
            get
            {
                return GetRequestValue<string>("CommonValue", "");

            }
        }

        protected string MachineType
        {
            get
            {
                return GetRequestValue<string>("MachineType", "");
            }
        }
        protected string DownlineDateFrom
        {
            get
            {
                return GetRequestValue<string>("DownlineDateFrom", "");

            }
        }
        protected string DownlineDateTo
        {
            get
            {
                return GetRequestValue<string>("DownlineDateTo", "");

            }
        }

        protected string beforeInStorageDate
        {
            get
            {
                return GetRequestValue<string>("beforeInStorageDate", "");
            }
        }

        protected string Remark
        {
            get
            {
                return GetRequestValue<string>("Remark", "");
            }
        }

        /// <summary>
        /// 排序字段 值为 id
        /// </summary>
        protected override string orderField
        {
            get
            {
                return string.IsNullOrEmpty(base.orderField) ? "JobId" : base.orderField;
            }
        }
        /// <summary>
        ///  排序方式 desc或 asc
        /// </summary>
        protected override string orderDirection
        {
            get
            {
                return string.IsNullOrEmpty(base.orderDirection) ? "DESC" : base.orderDirection;
            }
        }


        protected override string SQLSort
        {
            get
            {
                return (orderField + " " + orderDirection);
            }
        }

        #endregion



        protected string GetWhereSql()
        {
            string wheresql = QueryOperatorsEnumHelper.ConvertCommonOperator(CommonFiled, QueryOperatorsEnum.包含, CommonValue);
            string DOWNSQL = QueryOperatorsEnumHelper.ConvertCommonOperator("DownlineDate", QueryOperatorsEnum.范围, DownlineDateFrom + "|" + DownlineDateTo);

            if (!string.IsNullOrEmpty(wheresql) && !string.IsNullOrEmpty(DOWNSQL))
            {
                wheresql += " and " + DOWNSQL;
            }
            else if (!string.IsNullOrEmpty(DOWNSQL))
            {
                wheresql = DOWNSQL;
            }


            if (!MachineType.IsEmptyOrNull())
            {
                if (string.IsNullOrEmpty(wheresql))
                    wheresql = " MachineType='" + MachineType + "'";
                else
                    wheresql += " and MachineType='" + MachineType + "'";
            }

            if (!beforeInStorageDate.IsEmptyOrNull())
            {
                if (string.IsNullOrEmpty(wheresql))
                    wheresql = " OnlineDate>='" + beforeInStorageDate + "'";
                else
                    wheresql += " and OnlineDate>='" + beforeInStorageDate + "'";
            }


            if (!Remark.IsEmptyOrNull())
            {
                if (string.IsNullOrEmpty(wheresql))
                    wheresql = " Remark like '%" + Remark + "%'";
                else
                    wheresql += " and Remark like '%" + Remark + "%'";
            }

            return wheresql;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
           // Response.Write("请稍等,数据正在加载中...");
            //Response.Flush();
            //Response.ClearContent();
            //Response.Clear();
            int pageNum = 1;
            int numPerPage = 99999;
            int TotalCount = 0;
            DataTableCollection dtc = null;
            dtc = OJobController.GetPagination(GetWhereSql(), pageNum, numPerPage, SQLSort, ref TotalCount);
            export(dtc[0]);   
        }
        private void export(DataTable exportDT)
        {
            //DataTable exportDT = dts[0];
            InitializeWorkbook();
            //ISheet sheet1 = hssfworkbook.GetSheet(SheetName);
            
            //机器型号列表
            string[] mactypes = new string[] { "55NSL", "65NS", "65NSL", "68NS", "80NS", "55NS", "60NS", "55VXL", "80NC", "80NX", "40NS", "100NS", "100NST", "18VX" };
            int sheetindex = 1;//从第二个工作表开始
            foreach (string mactype in mactypes)
            {
                DataRow[] drs = exportDT.Select("MachineType='" + mactype + "'");
                if (drs.Count() <= 0)
                    break;
                DataTable dt=drs.CopyToDataTable();
                
                ISheet sheet2 = hssfworkbook.CloneSheet(0);
                hssfworkbook.SetSheetName(sheetindex, mactype);
                //FOR写入
                int rowindexBase = 1;
                int rowindex = 0;
                int rowReal = rowindex + rowindexBase;
                foreach (DataRow dr in dt.Rows)
                {
                    HSSFRow row1 = (HSSFRow)sheet2.CreateRow(rowReal);
                    row1.CreateCell(0).SetCellValue(dr["SerialNumber"].ToString());//编号
                    row1.CreateCell(1).SetCellValue(dr["EngineNumber"].ToString());//合同号
                    row1.CreateCell(2).SetCellValue(dr["DownlineDate"].ToString());//租赁公司
                    row1.CreateCell(3).SetCellValue(cbool(dr["GPSIsSetUp"]));//
                    row1.CreateCell(4).SetCellValue(dr["InStorageDate"].ToString());//入库日期
                    row1.CreateCell(5).SetCellValue(dr["GPSNumber"].ToString());//GPS号码
                    row1.CreateCell(6).SetCellValue(dr["OnlineDate"].ToString());//出库日期
                    row1.CreateCell(7).SetCellValue(cbool(dr["GPSOutStorage"]));//GPS出库
                    row1.CreateCell(8).SetCellValue(cbool(dr["MACStock"]));//整机库存
                    row1.CreateCell(9).SetCellValue(cbool(dr["GPSStock"]));//GPS库存
                    row1.CreateCell(10).SetCellValue(dr["OutStockType"].ToString());//出库形式
                    row1.CreateCell(11).SetCellValue(dr["AgentName"].ToString());//经销商
                    row1.CreateCell(12).SetCellValue(dr["LastOutStack"].ToString());//最后出库地点
                    row1.CreateCell(13).SetCellValue(dr["Remark"].ToString());//备注
                    rowindex++;
                    rowReal = rowindex + rowindexBase;
                }//[<=60] "差";>=90 "优";"中"           
                sheet2.ForceFormulaRecalculation = true;

                sheetindex++;
            }
            hssfworkbook.SetSheetHidden(0, true);
            hssfworkbook.SetActiveSheet(1);
            WriteToFile();
        }

        private int cbool(object obj)
        {
            if (obj == DBNull.Value)
                return 0;
            return Convert.ToInt32(obj);
        }

        protected override string GetXlsFileName(string oldFileName)
        {
            return string.Format("{0}_{1}.xls","机器库存一览表",DateTime.Now.ToString("yyyy-MM-dd"));
        }
    }
}